#
"""
什么是异常？
异常即是一个事件，该事件会在程序执行过程中发生，影响了程序的正常执行。
当Python脚本发生异常时我们需要捕获处理它，否则程序会终止执行
"""
# # 程序发生异常的例子
# print('程序开始运行啦')
# try:
#     print(1/0)  # 发生了一个异常 没有处理，所以程序在这里就停止了
# except:
#     print('程序有异常但是被捕捉到了，所以不会终止程序')
# print('程序还在运行')    # 接着运行
# 例子1
# try:
#     print(x)  # A代码块
# except:
#     print("发生了一个异常")  # B代码块


# print(1/0)  # ZeroDivisionError
# print(x)   # NameError
# 可以根据需要定义多种类型的异常块，
# 例如如果你想为一种指定的错误执行一段特殊的代码块：
# try:
#     print(1/0)
#     print(x)  # A
#
# except NameError:
#     print("Variable x is not defined,变量x未定义")  #
# except ZeroDivisionError:
#     print("触发了 ZeroDivisionError")
# except:
#     print('Something else went wrong 其他类型的错误发生了')
#
# try:
#     A代码块
# except:
#     B代码块
# else:
#     C代码块
# finally:
#     D代码块

# try:
#     print("Hello")
#     for i in range(10):
#         print(i)
# except:
#     print("Something went wrong，发生异常了")
# else:
#     print("Nothing went wrong，没有发生任何异常")  # C代码
# try:
#     x = 1
#     print(x)  # A
# except:
#     print("Something went wrong")  # B
# finally:
#     print("The 'try except' is finished")  # D

# try:
#     f = open("demofile.txt")
#     try:
#         f.write("Lorum Ipsum")
#     except:
#         print("Something went wrong when writing to the file")
#     finally:
#         f.close()
# except:
#     print("Something went wrong when opening the file")

# x = -1
#
# if x < 0:
#   raise Exception("不能小于0")

#
# x = "hello"
#
# if not type(x) is int:
#     raise TypeError("Only integers are allowed")
